const Service = require('../../../core/service/ApiService');

class DeviceDao extends Service {

    //获取所有设备
    async getAllDevice() {
        return this.model.Device.findAll({
            where: {
                status: {
                    $ne: this.constant.DEVICE_STATUS.DISABLE
                }
            },
            attributes:['device_id','device_pic','device_number','device_name','position','status'],
            order:[['status','ASC']],
            raw:true
        })
    }

    //获取设备
    async getDevice(status) {
        return this.model.Device.findAll({
            where: {
                status:status
            },
            attributes:['device_id','device_pic','device_name','device_number','position','mtime','status'],
            order:[['status','ASC']],
            raw:true

        })
    }

    //添加设备
    async addDevice(info) {
        return this.model.Device.create(info);
    }

    //获取设备具体信息
    async getDetails(device_id) {
        return this.model.Device.findOne({
            where: {
                device_id:device_id
            },
            attributes: ['device_id','device_pic','device_name','position','device_number','belong','details','status'],
            raw: true
        })
    }

    //搜索设备
    async searchDevice(name) {
        const sql = `select device_id,device_number,device_name,device_pic,position,belong_number,belong,type,status
                    from device
                    where device_name like '%${name}%' 
                    or type like '%${name}'
                    order by type`;
        return this.model.query(sql, {type: this.model.QueryTypes.SELECT});

    }
}

module.exports = DeviceDao;
